'use strict';
// 通过 require 引入 vk 实例
const vk = require('vk-unicloud');
// 通过 vk.init 初始化 vk实例（只有初始化后才能使用）
vk.init({
	baseDir: __dirname,
	requireFn: require
});
module.exports = {
	/**
	 * XXXnameXXX
	 * @url admin/pages/files_mzgl/dy_tv_series/kh/uploadVideo 前端调用的url参数地址
	 * data 请求参数
	 * @param {String} params1  参数1
	 */
	main: async (event) => {
		let {
			data = {}, userInfo, util, filterResponse, originalParam
		} = event;
		let {
			customUtil,
			uniID,
			config,
			pubFun,
			vk,
			db,
			_
		} = util;
		let {
			uid,
			_id,
			ma_app_id,
			video_meta
		} = data;
		let res = {
			code: 0,
			msg: ""
		};
		// 业务逻辑开始-----------------------------------------------------------
		
		let A = await vk.baseDao.selects({
		  dbName:"a_appid",
		  // 主表where条件
		  whereJson:{
		    appid:ma_app_id
		  },
		});
		
		console.log("A========",A);
		
		
		let appsecret = A.rows[0].appsecret

		

		let urlA = "https://open.douyin.com/oauth/client_token/"
		const accessToken = await uniCloud.httpclient.request(urlA, {
			method: 'POST',
			headers: {
				"Content-Type": "application/json",
			},
			data: {
				client_key: ma_app_id, // id
				client_secret: appsecret, // 密钥
				grant_type: "client_credential",
			},
			dataType: 'json', // 指定返回值为json格式，自动进行parseK
		})

		//创建短剧
		// 有效时间为 2 个小时
		let access_token = accessToken.data.data.access_token

		// 上传短剧至内容库
		let urlT = "https://open.douyin.com/api/playlet/v2/resource/upload/"
		const responseText = await uniCloud.httpclient.request(urlT, {
			method: 'POST',
			headers: {
				"Content-Type": "application/json",
				"access-token": access_token,
			},
			data: {
				resource_type: 1,
				ma_app_id: ma_app_id,
				video_meta: video_meta,
			},
			dataType: 'json', // 指定返回值为json格式，自动进行parseK
		})
		console.log("responseText", responseText);
		
		if (responseText.data.err_msg == "dy_cloud_id参数不合法，当前视频已上传" || responseText.data.err_msg == "dy_cloud_id参数不合法，已同步成功") {
			// 调用查询视频的接口
			console.log("11111111111111111111111", video_meta.dy_cloud_id, ma_app_id);
			let urlB = "https://open.douyin.com/api/playlet/v2/video/query/"
			const AAAA = await uniCloud.httpclient.request(urlB, {
				method: 'POST',
				headers: {
					"Content-Type": "application/json",
					"access-token": access_token,
				},
				data: {
					ma_app_id: ma_app_id,
					video_id_type: 2,
					dy_cloud_id: video_meta.dy_cloud_id
				},
				dataType: 'json', // 指定返回值为json格式，自动进行parseK
			})
			console.log("AAAA======", AAAA.data.data.open_video_id);
		
			// 填充当前openid
			let bbb = await vk.baseDao.update({
				dbName: "dy_tv_series", // 表名
				whereJson: { // 条件
					dy_cloud_id: video_meta.dy_cloud_id,
		
				},
				dataJson: { // 需要修改的数据
					open_video_id: AAAA.data.data.open_video_id,
					status: true
				}
			});
			
			
		return res;
		
		}
		
		let open_video_id = responseText.data.data.video_result.open_video_id
	
		// 执行 数据库 updateById 命令
		res.newdata = await vk.baseDao.updateById({
			dbName: "dy_tv_series",
			id: _id,
			dataJson: {
				open_video_id: open_video_id,
				status:true
			},
			getUpdateData: true,
		});

		// 业务逻辑结束-----------------------------------------------------------
		return res;
	}
}   